<?php
namespace Admin\Model;
use Think\Model;

class RoleModel extends Model{
	public $autoCheckFields        = false;
	public $_is_supper_admin       = false;//是否超级管理员
	public $_top_menu_list         = array();//顶部菜单
	public $_left_menu_list        = array();//左侧菜单
	public $_user_menu_left        = array();//用户左侧菜单
	public $_select_left_menu_info = array();//选中左侧菜单信息
	
	/**
	 * 网站栏目
	 * @param  string $company_id [description]
	 * @return [type]             [description]
	 */
	public function menu($role_id = '', $user_id = ''){
		$where['id'] = $role_id;
		$role_info = D('AdminRole')->where($where)->find();
		$role_auths = $role_info['auths'];
		$role_auths = unserialize($role_auths);
		unset($where);

		$where['id'] = $user_id;
		$user_auths = D('Admin')->where($where)->getField('auths');
		$user_auths = unserialize($user_auths);

		$menu = C('MENU_LIST');
		//最高权限不限制
		if($role_info['status'] == 99 || $role_id == 99){
			$this->_is_supper_admin = true;
			return $menu;
		}
		$role_menu = $this->auths_menu($role_auths, $menu);
		$user_menu = $this->auths_menu($user_auths, $menu);

		$merge_menu = $this->merge_menu($role_menu, $user_menu, $menu);
		// 重写栏目
		C('MENU_LIST', $merge_menu);
		return $merge_menu ? $merge_menu : array();
	}
	/**
	 * 过滤需要权限栏目
	 * @param  array $role_auths 要显示权限栏目
	 * @param  array $menu       所有栏目结构
	 */
	public function auths_menu($role_auths, $menu){
		$new_menu    = array();
		$filter_list = array();
		//根据权限配置显示栏目
		if($menu){
			//一级栏目处理
			foreach ($menu as $key => $menu_list) {
				//判断一级栏目是否有权限
				$cur_module = $role_auths[$key];
				if($cur_module){
					//二级栏目处理
					if($menu_list['list']){
						unset($list_2);
						foreach ($menu_list['list'] as $rs) {
							//三级栏目处理
							unset($list_3);
							foreach ($rs['list'] as $row) {
								$filter_list = array();
								if(in_array($row['url'], $cur_module)){
									foreach ($row['filter_list'] as $rw) {
										if(in_array($rw['url'], $cur_module)){
											$filter_list[] = $rw;
										}
									}
									$row['filter_list'] = $filter_list;
									$list_3[] = $row;
								}
							}
							if($list_3){
								$rs['list'] = $list_3;
								$list_2[]   = $rs;
							}
						}
						//重组后三级栏目第一个栏目URL
						$menu_list['url'] = $list_2[0]['list'][0]['url'];
						$menu_list['list'] = $list_2;
					}

					$new_menu[$key] = $menu_list;
				}
			}
		}
		return $new_menu;
	}
	/**
	 * 合并栏目权限（交集）
	 * @param  array $menu1     要显示栏目1
	 * @param  array $menu2     要显示栏目2
	 * @param  array $full_menu 全部栏目
	 */
	public function merge_menu($menu1, $menu2, $full_menu=array()){
		foreach ($menu1 as $key => $list) {
			foreach ($list['list'] as $rs) {
				foreach ($rs['list'] as $row) {
					$menu_auths[$key][] = $row['url'];
					foreach ($row['filter_list'] as $rw) {
						$menu_auths[$key][] = $rw['url'];
					}
				}
			}
		}
		foreach ($menu2 as $key => $list) {
			foreach ($list['list'] as $rs) {
				foreach ($rs['list'] as $row) {
					$menu_auths[$key][] = $row['url'];
					foreach ($row['filter_list'] as $rw) {
						$menu_auths[$key][] = $rw['url'];
					}
				}
			}
		}
		$merge_menu = $this->auths_menu($menu_auths, $full_menu);
		return $merge_menu ? $merge_menu : array();
	}
	//获取选中左侧菜单信息
	public function get_select_left_menu(){
		$select_info = array();
		$left_menu   = $this->get_user_menu_left();
		foreach ($left_menu as $rs) {
			foreach ($rs['list'] as $row) {
				if($row['is_select']){
					$select_info[]  = $rs;
					$select_info[] = $row;
					$this->_current_menu_info = $row;
					break;
				}
			}
		}
		$this->_select_left_menu_info = $select_info;
		return $select_info;
	}
	public function get_user_menu_top(){
		$list = C('MENU_LIST');
		foreach ($list as $rs) {
			$rs['is_select'] = $rs['is_select'] ? 1 : $this->is_select_menu($rs['url']);
			if(!$rs['is_select']){
				foreach ($rs['list'] as $row) {
					$rs['is_select'] = $rs['is_select'] ? 1 : $this->is_select_menu($row['url']);
					if(!$rs['is_select']){
						foreach ($row['list'] as $rw) {
							$rs['is_select'] = $rs['is_select'] ? 1 : $this->is_select_menu($rw['url']);
							foreach ($rw['filter_list'] as $fl) {
								$rs['is_select'] = $rs['is_select'] ? 1 : $this->is_select_menu($fl['url']);
							}
						}
						foreach ($row['filter_list'] as $fl) {
							$rs['is_select'] = $rs['is_select'] ? 1 : $this->is_select_menu($fl['url']);
						}
					}
				}
			}
			if($rs['is_select']){
				$this->_left_menu_list = $rs['list'];
			}
			$new_list[] = $rs;
		}
		$this->_top_menu_list = $new_list;
		return $new_list;
	}
	public function get_user_menu_left(){
		if(!$this->_left_menu_list){
			$this->get_user_menu_top();
		}
		$list = $this->_left_menu_list;
		foreach ($list as $rs) {
			$rs['is_select'] = $this->is_select_menu($rs['url']);
			foreach ($rs['filter_list'] as $fl) {
				$rs['is_select'] = $rs['is_select'] ? 1 : $this->is_select_menu($fl['url']);
			}
			$row_list = array();
			foreach ($rs['list'] as $row) {
				$row['is_select'] = $this->is_select_menu($row['url']);
				$fl_list = array();
				foreach ($row['filter_list'] as $fl) {
					$row['is_select'] = $row['is_select'] ? 1 : $this->is_select_menu($fl['url']);
					$rs['is_select']  = $row['is_select'] ? 1 : $rs['is_select'];
					$fl['is_select']  = $this->is_select_menu($fl['url']);
					$fl_list[] = $fl;
				}
				$row['filter_list'] = $fl_list;
				$rs['is_select'] = $row['is_select'] ? 1 : $rs['is_select'];
				$row_list[] = $row;
			}
			$rs['list'] = $row_list;
			$new_list[] = $rs;
		}
		$this->_user_menu_left = $new_list;
		return $new_list;
	}
	/**
	 * 是否选中菜单
	 * @param  [type]  $menu_url   [description]
	 * @param  [type]  $router_url [description]
	 * @return boolean             [description]
	 */
	public function is_select_menu($menu_url, $request_url = ''){
		// if($request_url){
		// 	var_dump($request_url . '===' . $menu_url);
		// }
		$mod_act     = str_replace(array('?', '=', '&'), '/', $menu_url);
		$request_url = $request_url ? $request_url : $_SERVER['REQUEST_URI'];
		$request_url = str_replace(array('?', '=', '&'), '/', $request_url);
		if($request_url == '/'){
			$request_url = 'Index/index';
		}
		// $request_url = str_replace(array('add', 'edit'), 'index', $request_url);
		// var_dump($request_url . '===' . $mod_act);
		if(strpos($request_url, $mod_act) !== false){
			return true;
		}
		return false;
	}
	// 获取用户栏目
	// _top_menu_list 顶部栏目
	// _user_menu_left 左侧栏目
	public function get_user_menu(){
		$this->get_user_menu_left();
		$this->get_select_left_menu();
		return $this->is_role();
	}
	// 是否有操作权限
	public function is_role(){
		if($this->_is_supper_admin){
			return true;
		}
		$is_role = false;
		foreach ($this->_top_menu_list as $rs) {
			if($rs['is_select']){
				$is_role = true;
			}
		}
		if(in_array(CONTROLLER_NAME, array('Login'))){
			$is_role = true;
		}
		if(in_array(ACTION_NAME, array('valid_field'))){
			$is_role = true;
		}
		// 退出登录权限过滤
		if(CONTROLLER_NAME == 'Index' && ACTION_NAME == 'logout'){
			$is_role = true;
		}
		return $is_role;
	}
	public function url_is_acl($url, $is_has_full_url = 0){
		$url = $is_has_full_url ? $url : U($url);
		$is_acl = false;

		foreach ($this->_top_menu_list as $rs) {
			if($this->is_select_menu($rs['url'], $url)){
				$is_acl = true;
				break;
			}
			foreach ($rs['filter_list'] as $fl) {
				if($this->is_select_menu($fl['url'], $url)){
					$is_acl = true;
					break;
				}
			}
			foreach ($rs['list'] as $row) {
				if($this->is_select_menu($row['url'], $url)){
					$is_acl = true;
					break;
				}
				foreach ($row['list'] as $row2) {
					if($this->is_select_menu($row2['url'], $url)){
						$is_acl = true;
						break;
					}
					foreach ($row2['filter_list'] as $fl) {
						if($this->is_select_menu($fl['url'], $url)){
							$is_acl = true;
							break;
						}
					}
				}
				foreach ($row['filter_list'] as $fl) {
					if($this->is_select_menu($fl['url'], $url)){
						$is_acl = true;
						break;
					}
				}
			}
		}
		// 退出登录权限过滤
		if(strpos($url, 'Index/logout') !== false){
			$is_acl = true;
		}
		return $is_acl ? 1 : 0;
	}
	public function is_acl($url, $is_has_full_url = 0){
		return $this->url_is_acl($url) ? '' : 'url="#delete-link"';
	}
}
?>